package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.JscxDaoljc;
import com.zr.model.Teacher;
import com.zr.util.JDBCUtil;

public class JscxDaoImplljc implements JscxDaoljc{
	//实现通过传入页码与数据个数获取当页的教师
	@Override
	public List<Teacher> getAllTeacherByPageAndRows(int page, int rows) {
		List<Teacher> teacher = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT teacher.tid,teacher.tname,teacher.tage,teacher.tsex,teacher.address,teacher.tpnumber");
		sql.append(" FROM teacher");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Teacher t = new Teacher();
				t.setTid(rs.getInt("tid"));
				t.setTname(rs.getString("tname"));
				t.setTage(rs.getString("tage"));
				t.setTsex(rs.getString("tsex"));
				t.setAddress(rs.getString("address"));
				t.setTpnumber(rs.getString("tpnumber"));
				teacher.add(t);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return teacher;
	}
	
	
	//返回教师总人数
	@Override
	public int countAllTeacher() {
		int count = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(teacher.tid) as count ");
		sql.append(" FROM teacher");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				count =Integer.parseInt(rs.getString("count"));
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

}
